package com.quanyan.stadium.service.gameRules;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.quanyan.stadium.biz.BizGameSystemDao;
import com.quanyan.stadium.constants.GameConstants;
import com.quanyan.stadium.entity.*;
import com.quanyan.stadium.entity.vo.GameGroup;
import com.quanyan.stadium.entity.vo.GameRound;
import com.quanyan.stadium.entity.vo.GameSchedule;
import com.quanyan.stadium.mapper.TbGameScheduleAddendumMapper;
import com.quanyan.stadium.mapper.TbTeamDoubleAddendumMapper;
import com.quanyan.stadium.service.commonComponent.BaseService;
import com.quanyan.stadium.service.gameRules.entity.GameRuleGenerate;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by xiaoxiao on 16/11/11.
 *
 * 赛制抽象类,定义赛制的一系列信息
 */
public abstract class AbsGameSystem {

    @Autowired
    private BaseService baseService;

    @Autowired
    private TbTeamDoubleAddendumMapper tbTeamDoubleAddendumMapper;

    @Autowired
    private BizGameSystemDao bizGameSystemDao;

    @Autowired
    private TbGameScheduleAddendumMapper tbGameScheduleAddendumMapper;

    /**
     * 赛事轮次创建,关键因素
     *      roundStatus   当前第几轮
     *      isGenerate    当前轮的对战表是否生成
     *      isEnd         当前轮次是否结束
     *
     *
     *      currentTeams 报名人数
     *
     *  params必备元素:
     *      1\tbGameEvent---- TbGameEvent对象 当前赛事
     *      2\teamId----- List<Integer> 队伍id集合(涉及到转化逻辑)
     *
     *
     *
     * */
    public abstract List<Integer> createGameRound(Integer gameEventId, Integer currentTeams, Map<String, Object> params) throws Exception;

    public abstract List<Integer> createGameSchedule(TbGameEvent tbGameEvent, GameRuleGenerate gameRuleGenerate);


    /**
     * 根据轮次id,更新轮次的信息
     *
     * */
    public void updateGameRound(){

    };

    /**
     * 根据赛程id,更新赛程的信息
     *
     * */
    public void updateSchedule() {

    };


    /**
     * 根据赛程id, 查询赛程的信息
     *
     * */
    public GameRound selectGameRound() {
        return null;
    };

    /**
     * 根据赛程id,查询赛程的信息
     *
     * */
    public GameSchedule selectSchedule() {
        return null;
    };

    /**
     * 根据赛事,获取赛事队伍列表
     *
     * */
    public List<Integer> getGameTeamList(TbGameEvent tbGameEvent, GameRuleGenerate gameRuleGenerate) {
        //对逻辑映射
        List<Integer> teamIds = new ArrayList<>();
        if (gameRuleGenerate.getIsDouble()) {
            TbTeamDoubleAddendumExample example = new TbTeamDoubleAddendumExample();
            TbTeamDoubleAddendumExample.Criteria criteria = example.createCriteria();
            criteria.andGameEventIdEqualTo(tbGameEvent.getId());
            criteria.andIsDeleteEqualTo(GameConstants.IS_NOT_DELETE);
            List<TbTeamDoubleAddendum> list = tbTeamDoubleAddendumMapper.selectByExample(example);
            for (TbTeamDoubleAddendum tbTeamDoubleAddendum : list) { //队伍id集合
                teamIds.add(tbTeamDoubleAddendum.getId());
            }
            gameRuleGenerate.setTbTeamDoubleAddendumList(list);
        }else {
            for (TbGameEventTeam tbGameEventTeam : gameRuleGenerate.getTbGameEventTeamList()) { //队伍id集合
                teamIds.add(tbGameEventTeam.getId());
            }
        }
        return teamIds;
    }

    /**
     *  生成赛事的对战表
     *
     * */
    public void generateScheduleAndUpdateRound(boolean isCreate, List<GameGroup> gameGroups, GameRuleGenerate reqChallenge) {
        if (isCreate) {
            if (null == gameGroups || gameGroups.size() < 1) {                   //校验list的合法性
                return;
            }
            for (GameGroup group : gameGroups) {
                TbGameSchedule schedule = new TbGameSchedule();
                schedule.setGameRoundId(group.getGameRoundId());
                schedule.setGroupType(group.getGroup().byteValue());
                if (group.getIsWin() || (null!=group.getTeamAwayId() && group.getTeamAwayId().intValue() == 0)) { //校验出现轮空的情况
                    schedule.setIsWin(GameConstants.IS_DELETE);
                    schedule.setStatus(GameConstants.RECORD_SUCCESS);
                    schedule.setIsEnd(GameConstants.IS_DELETE);
                    schedule.setFinalScore("1:0");
                }
                TbGameSchedule scheduleTemp = bizGameSystemDao.insertTbGameSchedule(schedule); //插入记录 对战记录
                if (reqChallenge.getIsDouble()) {
                    List<TbTeamDoubleAddendum> temp = reqChallenge.getTbTeamDoubleAddendumList();
                    Map<Integer, TbTeamDoubleAddendum> addendumMap = new HashMap<>();
                    for (TbTeamDoubleAddendum tbTeamDoubleAddendum : temp) {
                        addendumMap.put(tbTeamDoubleAddendum.getId(), tbTeamDoubleAddendum);
                    }

                    Gson gson = new Gson();
                    List<Integer> gameEventHomeTeamIds = gson.fromJson(addendumMap.get(group.getTeamHomeId()).getTeamIdGson() ,new TypeToken<List<Integer>>() {
                    }.getType());
                    for (Integer tempInt :gameEventHomeTeamIds) {
                        TbGameScheduleAddendum addendum1 = new TbGameScheduleAddendum();
                        addendum1.setGameScheduleId(scheduleTemp.getId());
                        addendum1.setType((byte) 0);
                        addendum1.setEventTeamId(tempInt);
                        tbGameScheduleAddendumMapper.insertSelective(addendum1);
                    }

                    List<Integer> gameEventAwayTeamIds = new ArrayList<>();
                    gameEventAwayTeamIds.add(0);
                    if (group.getTeamAwayId() != 0) {   //轮空逻辑处理
                        gameEventAwayTeamIds = gson.fromJson(addendumMap.get(group.getTeamAwayId()).getTeamIdGson() ,new TypeToken<List<Integer>>() {
                        }.getType());
                    }
                    for (Integer tempInt :gameEventAwayTeamIds) {
                        TbGameScheduleAddendum addendum2 = new TbGameScheduleAddendum();
                        addendum2.setGameScheduleId(scheduleTemp.getId());
                        addendum2.setType((byte) 1);
                        addendum2.setEventTeamId(tempInt);
                        tbGameScheduleAddendumMapper.insertSelective(addendum2);
                    }

                }else {
                    TbGameScheduleAddendum addendum1 = new TbGameScheduleAddendum();
                    addendum1.setGameScheduleId(scheduleTemp.getId());
                    addendum1.setType((byte) 0);
                    addendum1.setEventTeamId(group.getTeamHomeId());
                    TbGameScheduleAddendum addendum2 = new TbGameScheduleAddendum();
                    addendum2.setGameScheduleId(scheduleTemp.getId());
                    addendum2.setType((byte) 1);
                    addendum2.setEventTeamId(group.getTeamAwayId());
                    tbGameScheduleAddendumMapper.insertSelective(addendum1);
                    tbGameScheduleAddendumMapper.insertSelective(addendum2);
                }
            }
        }
    }




}
